package net.gq.logistics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.gq.logistics.pojo.*;
import net.gq.logistics.service.RoleService;
import net.gq.logistics.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/18
 */

@Controller
@RequestMapping("role")
public class roleController {

    @Resource(name = "roleService")
    private RoleService service;


    @Resource(name = "resultData")
    private ResultData rs;

    @Resource(name = "userService")
    private UserService userService;

    @RequestMapping("/rolePage")
    @RequiresPermissions("role:rolePage")
    public String rolePage() {

        return "rolePage";
    }


    /**
     * 分页，搜索排序
     *
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("role:list")

    @ResponseBody
    public PageInfo<Role> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,String keyword) {

        RoleExample example = new RoleExample();


        if (StringUtils.isNotBlank(keyword)) {

            //进行与名称账号比对
            RoleExample.Criteria criteria = example.createCriteria();
            criteria.andRolenameLike("%"+keyword+"%");

        }



        //开始分页
        PageHelper.startPage(pageNum, pageSize);

        List<Role> roles = service.selectByExample(example);

        //创建分页信息
        return new PageInfo<Role>(roles);

    }


    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultData delete(Long roleId) {


        /*
        删除前根据删除的角色id去管理员表查询是否还有管理员这个角色
        */

        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();

        criteria.andRoleIdEqualTo(roleId);

        List<User> users = userService.selectByExample(example);

        if (users.size() > 0) {
            rs=ResultData.createRs(0,"删除失败，存在子权限!");
            return rs;
        }

        //开始正常删除
        int result = service.deleteByPrimaryKey(roleId);

        if (result > 0) {

            rs = ResultData.createRs(1, "删除成功");
        } else {
            rs = ResultData.createRs(0, "删除失败");
        }

        return rs;

    }


    /**
     * 新增角色，编辑
     *
     *
     * @return
     */
    @RequestMapping("edit")
    public String  edit(Model model,Long roleId) {


        Role role = service.selectByPrimaryKey(roleId);

        model.addAttribute("role",role);

        return "roleEdit";
    }


    /** 新增
     * @param role
     * @return
     */
    @RequestMapping("insert")
    @ResponseBody
    public ResultData  role_add(Role role) {

        int result = service.insert(role);

        if (result > 0 ) {
            rs=ResultData.createRs(1,"插入成功！");
        } else {
            rs=ResultData.createRs(0,"插入失败！");
        }


        return rs;
    }


    /** 更新
     * @param role
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultData update(Role role) {

        int result = service.updateByPrimaryKeySelective(role);

        if (result > 0) {
            rs=ResultData.createRs(1,"修改成功！");
        } else {
            rs=ResultData.createRs(0,"修改失败!");
        }

        return rs;

    }


    /** 批量删除
     * @param str
     * @return
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public ResultData deleteAll(String str) {

        System.out.println(str.toString());

        int result=0;

        if (StringUtils.isNotBlank(str)) {
            //进行批量删除操作

            String[] strs = str.split(",");

            for (String s : strs) {
                result = service.deleteByPrimaryKey(Long.valueOf(s));
            }

            if (result > 0) {
                rs=ResultData.createRs(1,"删除成功！");
            } else {
                rs=ResultData.createRs(0,"删除失败！");
            }

        }

        return rs;


    }
}
